package com.jscloud.bigdata.flink.datastreamdemo;

import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * 定义1,2,3,4,5,6这些数字，通过map方法将每个数字乘以10，然后再使用filter方法将数字结果%2 == 0 的值给筛选出来
 * java版本实现
 */
public class MapAndFilterByJava {
        public static void main(String[] args) throws Exception {
                StreamExecutionEnvironment executionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment();
                DataStreamSource<Integer> sourceStream = executionEnvironment.fromElements(1, 2, 3, 4, 5, 6);

                SingleOutputStreamOperator<Integer> mapStream = sourceStream.map(new MapFunction<Integer, Integer>() {
                        @Override
                        public Integer map(Integer value) throws Exception {
                                return value * 10;
                        }
                });
                //过滤 数据
                SingleOutputStreamOperator<Integer> filterStream = mapStream.filter(new FilterFunction<Integer>() {
                        @Override
                        public boolean filter(Integer value) throws Exception {
                                return value % 2 == 0;
                        }
                });

                filterStream.print();
                executionEnvironment.execute();
        }

}
